<template>
  <div class="app">
    <van-nav-bar left-arrow @click-left="onClickLeft" />
    <img
      src="../../assets/img/邀请有礼.png"
      class="bigsimg"
      alt=""
      @click="chou"
    />
    <div class="mymoney">
      <span>我的金币</span>
      <img src="../../assets/img/金币.png" alt="" />
      <span>{{ num }}</span>
    </div>
    <ul>
      <li
        v-for="(item, index) in politelist"
        :key="index"
        @click="handleclick(item)"
      >
        <div class="juzhong">
          <img :src="item.img" class="bigimg" alt="" />
        </div>
        <div class="xinxi">
          <h6>{{ item.des }}</h6>
          <div>
            <span>已售{{ item.sell }}</span>
            <img src="../../assets/img/金币.png" alt="" />
            <b>{{ item.gold }}</b>
          </div>
        </div>
      </li>
    </ul>
    <div class="btn">
      <van-button
        round
        size="large"
        color="#ff9420"
        type="info"
        @click="showShare = true"
        >立即邀请</van-button
      >
      <van-share-sheet
        v-model:show="showShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
      />
    </div>
  </div>
</template>

<script>
import { Button, ShareSheet, NavBar, Notify, Toast, Dialog } from "vant";
import { computed, onMounted, ref } from "vue";
import { useStore } from "vuex";
export default {
  components: {
    vanNavBar: NavBar,
    vanButton: Button,
    vanShareSheet: ShareSheet,
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
  },
  setup() {
    // 获得金币
    const { options, showShare, onSelect, num } = glodfun();

    //金币商品数据渲染
    const { politelist } = rander();

    // 兑换商品功能
    const handleclick = (i) => {
      Dialog.confirm({
        message: "你确定要兑换这个" + i.des,
      })
        .then(() => {
          if (num.value >= +i.gold) {
            Toast.success("兑换成功");
            num.value = num.value - i.gold;
            localStorage.setItem("gold", num.value);
            console.log(i.gold);
            console.log(num.value > i.gold);
          } else {
            Toast.fail("金币不足，兑换失败");
          }
        })
        .catch(() => {
          // on cancel
        });
    };

    // 抽奖
    const chou = () => {
      Dialog.alert({
        message: "该功能还未开发,请上线后查看",
        theme: "round-button",
      }).then(() => {
        // on close
      });
    };

    return { politelist, options, showShare, onSelect, num, handleclick, chou };
  },
};

// 金币功能
const glodfun = () => {
  let num = ref("0");
  if (localStorage.getItem("gold")) {
    num.value = localStorage.getItem("gold");
  }
  const showShare = ref(false);
  const options = [
    [
      { name: "微信", icon: "wechat" },
      { name: "朋友圈", icon: "wechat-moments" },
      { name: "微博", icon: "weibo" },
      { name: "QQ", icon: "qq" },
    ],
  ];
  const onSelect = (option) => {
    Notify({ type: "success", message: "分享成功至" + option.name });
    localStorage.setItem("gold", +localStorage.getItem("gold") + 10);
    Toast.success("金币+10");
    num.value = localStorage.getItem("gold");
    showShare.value = false;
  };
  return { options, showShare, onSelect, num };
};

// 商品渲染
const rander = () => {
  const store = useStore();
  const politelist = computed(() => {
    return store.state.polise.politelist;
  });
  onMounted(() => {
    store.dispatch("getpoliteAsync");
  });
  return { politelist };
};
</script>

<style lang="less" scoped>
.app {
  margin: auto;
  background: #f5f5f5;
  .bigsimg {
    display: block;
    margin: auto;
  }
  .mymoney {
    width: 347px;
    margin: auto;
    span {
      font-size: 16px;
    }
    img {
      margin-left: 30px;
      margin-right: 10px;
    }
  }
  ul {
    width: 356px;
    margin: 26px auto;
    &:after {
      content: "";
      clear: both;
      display: block;
      height: 0;
      overflow: hidden;
      visibility: hidden;
    }
    li {
      width: 164px;
      height: 244px;
      float: left;
      background: white;
      margin: 0 7px 20px;
      box-shadow: 0px 0px 5px 5px rgba(0, 0, 0, 0.2);
      border-radius: 20px;
      overflow: hidden;
      .juzhong {
        height: 166px;
        width: 166px;
        position: relative;
      }
      .bigimg {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
      .xinxi {
        background: #f5f5f5;
        height: 78px;
        padding: 13px;
        h6 {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        div {
          position: relative;
          span {
            font-size: 12px;
          }

          img {
            position: absolute;
            top: 50%;
            right: 20%;
            transform: translate(0, -25%);
          }
          b {
            font-size: 12px;
            position: absolute;
            top: 50%;
            right: 0;
            transform: translate(0, -25%);
          }
        }
      }
    }
  }
  .btn {
    width: 345px;
    height: 50px;
    margin: auto;
  }
}
</style>
